package com.ss.android.article.base.feature.video.cdn.cdn;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;

import com.bytedance.common.utility.Logger;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.video.cdn.cdnOptimize.ISSCdnTachometerCallback;
import com.ss.android.article.base.feature.video.cdn.cdnOptimize.SSCdnTachometerTask;
import com.ss.android.article.base.feature.video.cdn.data.SSCdnCacheItem;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.newmedia.BaseApplication;
import com.ss.ttvideoengine.b.VideoItem;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SSCdnIpConnect implements ISSCdnTachometerCallback {
    class SortItem {
        public int rank;
        public String url;

        public SortItem(String arg1, int arg2) {

            this.url = arg1;
            this.rank = arg2;
        }
    }

    private static final int CDN_URL_TEST_START = 10001;
    private static final int CDN_URL_TEST_START_TIME = 10000;
    private static final String TAG = "CdnTachometer";
    private Context applicationContext;
    private SSCdnCacheItem cacheItem;
    private final Handler handler;
    private static SSCdnIpConnect instance;

    static {
        SSCdnIpConnect.instance = null;
    }

    private SSCdnIpConnect() {

        this.applicationContext = null;
        this.cacheItem = null;
        this.handler = new Handler(Looper.getMainLooper()){
            public void handleMessage(Message arg3) {
                super.handleMessage(arg3);
                if(arg3.what == 10001) {
                    SSCdnIpConnect.inst().startTachometerCdnList();
                }
            }
        };
        this.init();
    }

    public String getIpUrl(String arg3) {
        String v0 = null;
        if(this.cacheItem != null && (this.cacheItem.canSocreUrls())) {
            if(this.cacheItem.isLossEfficacy()) {
                this.reSetTachometerCdnList();
            }
            else {
                v0 = "";
            }
        }

        return v0;
    }

    private static String getNetIdentifier(Context arg2) {
        if(NetworkUtils.isWifi(arg2)) {
            try {
                Object v0_2 = arg2.getSystemService(Context.WIFI_SERVICE);
                if(!((WifiManager)v0_2).isWifiEnabled()) {
                    ((WifiManager)v0_2).setWifiEnabled(true);
                }

                String v0_3 = ((WifiManager)v0_2).getConnectionInfo().getSSID();
                return v0_3;
            }
            catch(Error v0) {
            }
            catch(Exception v0_1) {
            }
        }

        return null;
    }

    public void init() {
        this.resetCacheItem();
        this.applicationContext = BaseApplication.getApplication().getApplicationContext();
        this.handler.sendEmptyMessageAtTime(10001, 10000);
    }

    public static SSCdnIpConnect inst() {
        if(SSCdnIpConnect.instance == null) {
            Class v1 = SSCdnIpConnect.class;
            synchronized (v1){
                if(SSCdnIpConnect.instance == null) {
                    SSCdnIpConnect.instance = new SSCdnIpConnect();
                }
            }
        }

        return SSCdnIpConnect.instance;
    }

    public void onTachometerFinish(boolean arg3) {
        if(this.cacheItem != null) {
            this.cacheItem.isTachometering = false;
            this.cacheItem.isSuccess = arg3;
        }
    }

    private void reSetTachometerCdnList() {
        int v1 = 10001;
        if(!this.handler.hasMessages(v1)) {
            this.handler.sendEmptyMessageAtTime(v1, 10000);
        }
    }

    public void resetCacheItem() {
        this.cacheItem = new SSCdnCacheItem();
        this.cacheItem.extractFields(null);
    }

    public void scoreVideoInfoUrls(VideoItem arg8) {
        int v6 = 2;
        if((AppData.getInstance().getAbSettings().isOpenVideoIpEnable()) && arg8 != null && (NetworkUtils.isWifi(this.applicationContext)) && this.cacheItem != null && (this.cacheItem.canSocreUrls())) {
            if(this.cacheItem.isLossEfficacy()) {
                this.reSetTachometerCdnList();
            }
            else {
                ArrayList v1 = new ArrayList();
                if(!TextUtils.isEmpty(arg8.mainUrl)) {
                    v1.add(new SortItem(arg8.mainUrl, this.cacheItem.scoreUrl(arg8.mainUrl)));
                    if(!TextUtils.isEmpty(arg8.backupUrl1)) {
                        v1.add(new SortItem(arg8.backupUrl1, this.cacheItem.scoreUrl(arg8.backupUrl1)));
                        if(!TextUtils.isEmpty(arg8.backupUrl2)) {
                            v1.add(new SortItem(arg8.backupUrl2, this.cacheItem.scoreUrl(arg8.backupUrl2)));
                            if(!TextUtils.isEmpty(arg8.backupUrl3)) {
                                v1.add(new SortItem(arg8.backupUrl3, this.cacheItem.scoreUrl(arg8.backupUrl3)));
                                this.sortList(v1);
                                arg8.mainUrl = ((SortItem)v1.get(0)).url;
                                arg8.backupUrl1 = ((SortItem)v1.get(1)).url;
                                arg8.backupUrl2 = ((SortItem)v1.get(v6)).url;
                                arg8.backupUrl3 = ((SortItem)v1.get(3)).url;
                            }
                            else {
                                this.sortList(v1);
                                arg8.mainUrl = ((SortItem)v1.get(0)).url;
                                arg8.backupUrl1 = ((SortItem)v1.get(1)).url;
                                arg8.backupUrl2 = ((SortItem)v1.get(v6)).url;
                            }
                        }
                        else {
                            this.sortList(v1);
                            arg8.mainUrl = ((SortItem)v1.get(0)).url;
                            arg8.backupUrl1 = ((SortItem)v1.get(1)).url;
                        }
                    }
                }
            }
        }
    }

    private void sortList(ArrayList arg3) {
        if(arg3 != null && arg3.size() > 1) {
            Collections.sort(((List)arg3), new Comparator<SortItem>(){
                public int compare(SortItem arg3, SortItem arg4) {
                    int v0 = arg3.rank > arg4.rank ? 1 : 0;
                    return v0;
                }
            });
        }
    }

    public void startTachometerCdnList() {
        int v0;
        if(this.cacheItem != null && !this.cacheItem.isTachometering) {
            Logger.d("CdnTachometer", "startTachometerCdnList()");
            long v4 = System.currentTimeMillis();
            String v3 = SSCdnIpConnect.getNetIdentifier(this.applicationContext);
            if(this.cacheItem.identifier == null || v3 == null || (this.cacheItem.identifier.equals(v3))) {
                v0 = 0;
            }
            else {
                this.resetCacheItem();
                v0 = 1;
            }

            if(!NetworkUtils.isWifi(this.applicationContext)) {
                return;
            }

            if(v0 == 0 && this.cacheItem.lastTachometerTime != 0 && !this.cacheItem.isLossEfficacy()) {
                return;
            }

            this.cacheItem.lastTachometerTime = v4;
            this.cacheItem.identifier = v3;
            this.cacheItem.isTachometering = true;
            new SSCdnTachometerTask(((ISSCdnTachometerCallback)this), this.cacheItem).execute(new Object[0]);
        }
    }
}

